import request from '@/utils/request'

export interface BillDTO {
  billId?: string
  billNo?: string
  billType: 'PO_IN' | 'SO_OUT'
  orgId?: string
  warehouseId: string
  status?: string
  remark?: string
}

export interface BillItemDTO {
  id?: string
  billId?: string
  lineNo?: number
  materialId: string
  uomId: string
  qty: number
  price?: number
  amount?: number
  lotId?: string
  serialId?: string
  remark?: string
}

export function pageBills(params: { pageNum: number; pageSize: number; billType: string; status?: string; billNo?: string }) {
  return request({ url: '/erp/bill/page', method: 'get', params })
}

export function getBill(id: string) {
  return request({ url: `/erp/bill/${id}`, method: 'get' })
}

export function createBill(data: BillDTO) {
  return request({ url: '/erp/bill', method: 'post', data })
}

export function updateBill(id: string, data: BillDTO) {
  return request({ url: `/erp/bill/${id}`, method: 'put', data })
}

export function submitBill(id: string) {
  return request({ url: `/erp/bill/${String(id)}/submit`, method: 'post' })
}

export function approveBill(id: string) {
  return request({ url: `/erp/bill/${String(id)}/approve`, method: 'post' })
}

export function postBill(id: string) {
  return request({ url: `/erp/bill/${String(id)}/post`, method: 'post' })
}

export function getBillProcId(id: string) {
  return request({ url: `/erp/bill/${String(id)}/proc`, method: 'get' })
}

export function getNextBillNo(billType: string) {
  return request({ url: '/erp/bill/nextNo', method: 'get', params: { billType } })
}

export function addBillItem(billId: string, data: BillItemDTO) {
  return request({ url: `/erp/bill/${String(billId)}/items`, method: 'post', data })
}

export function listBillItems(billId: string) {
  return request({ url: `/erp/bill/${String(billId)}/items`, method: 'get' })
}

export function updateBillItem(billId: string, itemId: string, data: BillItemDTO) {
  return request({ url: `/erp/bill/${String(billId)}/items/${String(itemId)}`, method: 'put', data })
}

export function deleteBillItem(billId: string, itemId: string) {
  return request({ url: `/erp/bill/${String(billId)}/items/${String(itemId)}`, method: 'delete' })
}

export function saveSubmit(data: { bill: BillDTO; items: BillItemDTO[]; submit: boolean }) {
  return request({ url: '/erp/bill/save-submit', method: 'post', data })
}


